package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdComm;
import com.powernode.model.Result;
import com.powernode.service.ProdCommService;
import com.powernode.vo.ProdCommData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 商品评论业务控制层
 */
@Api(tags = "商品评论接口管理")
@RequestMapping("prod/prodComm")
@RestController
public class ProdCommController {

    @Autowired
    private ProdCommService prodCommService;

    @ApiOperation("多条件分页查询商品评论")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(Page<ProdComm> page,ProdComm prodComm) {
        page = prodCommService.page(page,new LambdaQueryWrapper<ProdComm>()
                .eq(ObjectUtil.isNotNull(prodComm.getStatus()),ProdComm::getStatus,prodComm.getStatus())
                .like(StringUtils.hasText(prodComm.getProdName()),ProdComm::getProdName,prodComm.getProdName())
                .orderByDesc(ProdComm::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("根据标识查询商品评论详情")
    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@PathVariable Long id) {
        ProdComm prodComm = prodCommService.getById(id);
        return Result.success(prodComm);
    }

    @ApiOperation("回复并审核评论")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<String> replyAndExamineProdComm(@RequestBody ProdComm prodComm) {
        Integer count = prodCommService.replyAndExamineProdComm(prodComm);
        return Result.handle(count>0);
    }

    /////////////////////// 微信小程序 ///////////////////////////////
//    prod/prodComm/prodComm/prodCommData?prodId=101
    @ApiOperation("查询单个商品评论总览信息")
    @GetMapping("prodComm/prodCommData")
    public Result<ProdCommData> loadProdCommData(@RequestParam Long prodId) {
        ProdCommData prodCommData = prodCommService.queryProdCommDataByProdId(prodId);
        return Result.success(prodCommData);
    }

//    prod/prodComm/prodComm/prodCommPageByProd?prodId=101&size=10&current=1&evaluate=-1
    @ApiOperation("分页查询单个商品的评论")
    @GetMapping("prodComm/prodCommPageByProd")
    public Result<Page<ProdComm>> loadProdCommPageByProd(@RequestParam Long prodId,
                                                         @RequestParam Long current,
                                                         @RequestParam Long size,
                                                         @RequestParam Long evaluate) {
        Page<ProdComm> page = prodCommService.queryProdProdPageByProd(prodId,current,size,evaluate);
        return Result.success(page);
    }
}
